package com.junyi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * Map + 函数式接口
 * Map + 函数式接口 相比较与 if-else 的业务逻辑代码，能够避免策略模式带来的类暴增、整个业务逻辑分散在多个类的问题
 * @time: 2022/1/20 11:48
 * @version: 1.0
 * @author: junyi Xu
 */
public class BookService3 {

    @Autowired
    DiscountService discountService;

    private Map<String, Function<String, String>> map = new HashMap<>();

    @PostConstruct
    public void init() {
        map.put("1", type -> discountService.moneyDiscount(type));
        map.put("2", type -> discountService.vipDiscount(type));
        map.put("3", type -> discountService.moneyDiscountCondition(type));
    }

    public void getByType() {
        String type = "1";

        Function<String, String> function = map.get(type);
        if (function != null) {
            function.apply(type);
        }
    }


    @Service
    public class DiscountService {

        public String moneyDiscount(String type) {
            return "";
        }

        public String vipDiscount(String type) {
            return "";
        }

        public String moneyDiscountCondition(String type) {
            return "";
        }
    }
}
